home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / non-internet / samba / source / charcnv.c < prev    next >
C/C++ Source or Header  |  1996-06-26  |  3KB  |  127 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    Character set conversion Extensions
  5.    Copyright (C) Andrew Tridgell 1992-1994
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. */
  22. #include "includes.h"
  23. extern int DEBUGLEVEL;
  24.  
  25. static char cvtbuf[1024];
  26.  
  27. static mapsinited = 0;
  28.  
  29. static char unix2dos[256];
  30. static char dos2unix[256];
  31.  
  32. static void initmaps() {
  33.     int k;
  34.  
  35.     for (k = 0; k < 256; k++) unix2dos[k] = k;
  36.     for (k = 0; k < 256; k++) dos2unix[k] = k;
  37.  
  38.     mapsinited = 1;
  39. }
  40.  
  41. static void update_map(char * str) {
  42.     char *p;
  43.  
  44.     for (p = str; *p; p++) {
  45.         if (p[1]) {
  46.             unix2dos[(unsigned char)*p] = p[1];
  47.             dos2unix[(unsigned char)p[1]] = *p;
  48.             p++;
  49.         }
  50.     }
  51. }
  52.  
  53. static void initiso() {
  54.  
  55.     if (!mapsinited) initmaps();
  56.  
  57.     update_map("\241\255\242\233\243\234\244\236\245\235\246\272\247\025\250\251");
  58.     update_map("\251\273\252\246\253\256\254\252\255\274\256\310\257\257\260\370");
  59.     update_map("\261\361\262\375\263\264\264\265\265\266\266\024\267\371\270\267");
  60.     update_map("\271\270\272\247\273\275\274\254\275\253\276\276\277\250\200\277");
  61.     update_map("\301\300\302\301\303\302\304\216\305\217\306\222\307\200\310\303");
  62.     update_map("\311\220\312\305\313\306\314\307\315\315\316\317\317\320\320\311");
  63.     update_map("\321\245\322\321\323\322\324\323\325\324\326\231\327\312\330\325");
  64.     update_map("\331\326\332\327\333\330\334\232\335\313\336\314\337\341\340\205");
  65.     update_map("\341\240\342\203\343\331\344\204\345\206\346\221\347\207\350\212");
  66.     update_map("\351\202\352\210\353\211\354\215\355\241\356\214\357\213\360\316");
  67.     update_map("\361\244\362\225\363\242\364\223\365\332\366\224\367\366\370\362");
  68.     update_map("\371\227\372\243\373\226\374\201\375\304\376\263\377\230");
  69. }
  70.  
  71. /*
  72.  * Convert unix to dos
  73.  */
  74. char *
  75. unix2dos_format(char *str,BOOL overwrite)
  76. {
  77.     char *p;
  78.     char *dp;
  79.  
  80.     if (!mapsinited) initmaps();
  81.     if (overwrite) {
  82.         for (p = str; *p; p++) *p = unix2dos[(unsigned char)*p];
  83.         return str;
  84.     } else {
  85.         for (p = str, dp = cvtbuf; *p; p++,dp++) *dp = unix2dos[(unsigned char)*p];
  86.         *dp = 0;
  87.         return cvtbuf;
  88.     }
  89. }
  90.  
  91. /*
  92.  * Convert dos to unix
  93.  */
  94. char *
  95. dos2unix_format (char *str, BOOL overwrite)
  96. {
  97.     char *p;
  98.     char *dp;
  99.  
  100.     if (!mapsinited) initmaps();
  101.     if (overwrite) {
  102.         for (p = str; *p; p++) *p = dos2unix[(unsigned char)*p];
  103.         return str;
  104.     } else {
  105.         for (p = str, dp = cvtbuf; *p; p++,dp++) *dp = dos2unix[(unsigned char)*p];
  106.         *dp = 0;
  107.         return cvtbuf;
  108.     }
  109. }
  110.  
  111.  
  112. /*
  113.  * Interpret character set.
  114.  */
  115. int 
  116. interpret_character_set (char *str, int def)
  117. {
  118.  
  119.     if (strequal (str, "iso8859-1")) {
  120.         initiso();
  121.         return def;
  122.     } else {
  123.         DEBUG(0,("unrecognized character set\n"));
  124.     }
  125.     return def;
  126. }
  127.